﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LittleBirdAuction.Core
{
    public  class Support
    {
        public static void IsolateGemBonuses( ref DiabloItem item )
        {
            //TODO            
        }

        /**/
        public static bool SyncGemInfo( ref Stat stat )
        {
            stat.gem_type = GemTypeId.GEM_TYPE_EMPTY;
            stat.gem_rank = 0;

            if(stat.values.Count > 0)
            {
                uint       value = (uint)(stat.values[0]);
                GemStatId stat_id;
                IdEnum item = null;

                // parse gem stat
                if (!AuctionConst.ITEM_GEM_STAT_IDS.FindObject(out item, stat.text, true))
                    return false;

                stat_id = (GemStatId)item.Value;

                // get type from stat id
                switch(stat_id)
                {
                // amethyst
                case GemStatId.GEM_STAT_VITALITY:
                case GemStatId.GEM_STAT_LIFEP:
                case GemStatId.GEM_STAT_LIFEONHIT:
                    stat.gem_type = GemTypeId.GEM_TYPE_AMETHYST;
                    break;

                // emerald
                case GemStatId.GEM_STAT_DEXTERITY:
                case GemStatId.GEM_STAT_GOLDFIND:
                case GemStatId.GEM_STAT_CRITICALHITDAMAGE:
                    stat.gem_type = GemTypeId.GEM_TYPE_EMERALD;
                    break;

                // ruby
                case GemStatId.GEM_STAT_STRENGTH:
                case GemStatId.GEM_STAT_BONUSEXPERIENCE:
                case GemStatId.GEM_STAT_AVERAGEDAMAGE:
                    stat.gem_type = GemTypeId.GEM_TYPE_RUBY;
                    break;

                // topaz
                case GemStatId.GEM_STAT_INTELLIGENCE:
                case GemStatId.GEM_STAT_MAGICFIND:
                case GemStatId.GEM_STAT_PHYSICALDAMAGETOATTACKER:
                    stat.gem_type = GemTypeId.GEM_TYPE_TOPAZ;
                    break;

                default:
                    return false;
                }

                // get rank from stat value
                switch(stat_id)
                {
                case GemStatId.GEM_STAT_VITALITY:
                case GemStatId.GEM_STAT_DEXTERITY:
                case GemStatId.GEM_STAT_STRENGTH:
                case GemStatId.GEM_STAT_INTELLIGENCE:
                    stat.gem_rank = ((value - 6) / 4 + 1);
                    break;
                
                case GemStatId.GEM_STAT_GOLDFIND:
                case GemStatId.GEM_STAT_MAGICFIND:
                case GemStatId.GEM_STAT_BONUSEXPERIENCE:
                    stat.gem_rank = ((value - 5) / 2 + 1);
                    break;

                case GemStatId.GEM_STAT_LIFEP:
                    stat.gem_rank = (value - 4);
                    break;

                case GemStatId.GEM_STAT_LIFEONHIT:
                    stat.gem_rank = (uint)(Array.IndexOf(AuctionConst.ITEM_GEM_LIFEONHIT, value) + 1);
                    break;

                case GemStatId.GEM_STAT_CRITICALHITDAMAGE:
                    stat.gem_rank = (uint)(Array.IndexOf(AuctionConst.ITEM_GEM_CRITICALHITDAMAGE, value) + 1);
                    break;

                case GemStatId.GEM_STAT_AVERAGEDAMAGE:
                    stat.gem_rank = (uint)(Array.IndexOf(AuctionConst.ITEM_GEM_BONUSMINIMUMWEAPONDAMAGE, value) + 1);
                    break;

                case GemStatId.GEM_STAT_PHYSICALDAMAGETOATTACKER:
                    stat.gem_rank = (uint)(Array.IndexOf(AuctionConst.ITEM_GEM_PHYSICALDAMAGETOATTACKER, value) + 1);
                    break;
                }

                // sanity check
                if(stat.gem_rank > AuctionConst.ITEM_GEM_RANK_LIMIT)
                    stat.gem_rank = 0;
            }

            return true;
        }

        /**/
        public static void StripGemBonuses( ref DiabloItem item )
        {
            // if weapon
            if(item.damage_min > 0 && item.damage_max >0 && item.damage_aps>0)
            {
                bool adjusted = false;

                // for each gem
                foreach( Stat i in item.sockets)
                {
                    // reduce damage by ruby bonus
                    if (i.gem_type == GemTypeId.GEM_TYPE_RUBY && i.gem_rank > 0 && i.gem_rank <= AuctionConst.ITEM_GEM_RANK_LIMIT)
                    {
                        uint damage = AuctionConst.ITEM_GEM_BONUSMINIMUMWEAPONDAMAGE[i.gem_rank - 1];
                        string  damage_string = string.Empty;

                        // check damage bonus modifier
                        if (AuctionConst.ITEM_HOVER_STAT_IDS.FindName(ref damage_string, (uint)HoverStatId.HOVER_STAT_PERCENTDAMAGE))
                        {
                            Stat stat = new Stat();

                            if (item.FindStat(damage_string, false, out stat))
                            {
                                Double multiplier = 1.0 + stat.values[0] / 100.0;

                                // adjust gem damage
                                damage = (uint)(damage * multiplier);
                            }
                        }

                        // parse gem stat
                        item.damage_min -= damage;
                        item.damage_max -= damage;

                        adjusted = true;
                    }
                }

                // recalculate dps
                if(adjusted)
                    item.dpsarmor = ((item.damage_min + item.damage_max) * item.damage_aps / (ulong)AuctionConst.NUMBER_ACCURACY) / 2;
            }
        }
        /**/
        public static void BuildItemHash(ref DiabloItem item)
        {
            string id = item.hash;

            // include name
            id = item.name;


            foreach (Stat stat in item.stats)
            {
                id += string.Format("|%{0}:%{1}", stat.text, stat.values.Count > 0 ? stat.values[0] : 0);
            }

            item.hash = id;
        }
    }
}
